package com.ts.proj.controller;

import com.ts.base.controller.BaseController;
import com.ts.common.constant.ConsoleConstant;
import com.ts.proj.entity.ProjectEntity;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.json.SystemConstantJson;
import com.ts.proj.service.ProjectService;
import com.ts.util.JsonUtils;
import com.ts.util.JurisdictionUtil;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//说明 字符串：Test，作为替换字符串

@Controller
public class ProjectController extends BaseController {

    @Autowired
    private SystemConstantJson systemConstantJson;

    @Autowired
    private ProjectService progectService;
    @RequestMapping("/project-add")
    public ModelAndView projectAddManage() {
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        String flag = "add";
        if(JurisdictionUtil.isValid()){
            HttpSession session = this.getRequest().getSession();
            SystemUserEntity user = JsonUtils.jsonToObject(session.getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            PageData pd = this.getPageData();
            ProjectEntity project = new ProjectEntity();
            if(null!=pd.get("id")&&!"".equals(pd.getString("id"))){
                project = progectService.findById(Integer.valueOf(pd.getString("id")));
                flag = "edit";
            }
            if("copy".equals(pd.getString("sql"))){
                project.setId(-1);
            }
            List<PageData> proStateList = new ArrayList<>();
            proStateList = (List<PageData>) systemConstantJson.constantsByType("proj_state");
            modelAndView.addObject("proj_state",proStateList);
            modelAndView.addObject("flag",flag);
            modelAndView.addObject("project",project);
            viewName = "business/project/project-add";
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

}